package com.omesoft.hksnore.util.titlebar;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.omesoft.hksnore.R;


public class MyTitlebarUtil {

    public static final String BACKTITLE = "backTitle";
    public static int STATE_BAR_HEIGHT = 0;

    public static void setBackgroundTransparent(Activity activity) {
        LinearLayout bg = (LinearLayout) activity.findViewById(R.id.titlebar);
        bg.setBackgroundColor(activity.getResources().getColor(R.color.transparent));
    }

    // 标题========================================================================

    /**
     * 显示标题
     *
     * @param activity  当前的activity
     * @param titleName 标题
     * @return
     */
    public static TextView showTitleName(Activity activity, String titleName) {
        TextView tvTitleName = (TextView) activity.findViewById(R.id.title_tv);
        tvTitleName.setText(titleName);
        tvTitleName.setVisibility(View.VISIBLE);
        setStateHight(activity);
        return tvTitleName;
    }

    public static TextView showTitleName5(Activity activity, String titleName) {
        TextView tvTitleName = (TextView) activity.findViewById(R.id.title_tv);
        tvTitleName.setText(titleName);
        tvTitleName.setVisibility(View.VISIBLE);
        return tvTitleName;
    }

    public static TextView showTitleRightName(Activity activity, String titleName) {
        TextView tvTitleName = (TextView) activity.findViewById(R.id.title_tv_right);
        tvTitleName.setText(titleName);
        tvTitleName.setVisibility(View.VISIBLE);
//		setStateHight(activity);
        return tvTitleName;
    }

    public static void setStateHight(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (STATE_BAR_HEIGHT <= 0) {
                STATE_BAR_HEIGHT = getStatusHeight(activity);
            }
            Window window = activity.getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            View view = activity.findViewById(R.id.state_bar);
            view.setVisibility(View.VISIBLE);
            LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) view.getLayoutParams();
            linearParams.height = STATE_BAR_HEIGHT;
            view.setLayoutParams(linearParams);

        }
//        StatusBarUtil.StatusBarLightMode(activity);
    }

    // public static void setMainTitleBg(Activity activity, LinearLayout
    // mLinearLayout) {
    // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    // if (STATE_BAR_HEIGHT <= 0) {
    // STATE_BAR_HEIGHT = getStatusHeight(activity);
    // }
    // if (STATE_BAR_HEIGHT > 0) {
    // mLinearLayout.setBackgroundColor(activity.getResources().getColor(R.color.bg_title_blue));
    // }
    // }
    // }

    public static void setStateHeight(Activity activity, View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (STATE_BAR_HEIGHT <= 0) {
                STATE_BAR_HEIGHT = getStatusHeight(activity);
            }
            Window window = activity.getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            view.setVisibility(View.VISIBLE);
            LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) view.getLayoutParams();
            // Log.e("titlebar", "STATE_BAR_HEIGHT：：：" + STATE_BAR_HEIGHT);
            linearParams.height = STATE_BAR_HEIGHT;
            view.setLayoutParams(linearParams);
        } else {
            // Log.e("titlebar", "版本过低：：：");
            view.setVisibility(View.GONE);
        }
        StatusBarUtil.StatusBarLightMode(activity);
    }

    public static void setStateHeightByRl(Activity activity, View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (STATE_BAR_HEIGHT <= 0) {
                STATE_BAR_HEIGHT = getStatusHeight(activity);
            }
            Window window = activity.getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            view.setVisibility(View.VISIBLE);
            RelativeLayout.LayoutParams linearParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
            // Log.e("titlebar", "STATE_BAR_HEIGHT：：：" + STATE_BAR_HEIGHT);
            linearParams.height = STATE_BAR_HEIGHT;
            view.setLayoutParams(linearParams);
        } else {
            // Log.e("titlebar", "版本过低：：：");
            view.setVisibility(View.GONE);
        }
        StatusBarUtil.StatusBarLightMode(activity);
    }

    public static TextView showTitleNameByRl(Activity activity, String titleName) {
        TextView tvTitleName = (TextView) activity.findViewById(R.id.title_tv);
        tvTitleName.setText(titleName);
        tvTitleName.setVisibility(View.VISIBLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (STATE_BAR_HEIGHT <= 0) {
                STATE_BAR_HEIGHT = getStatusHeight(activity);
            }
            Window window = activity.getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            View view = activity.findViewById(R.id.state_bar);
            view.setVisibility(View.VISIBLE);
            LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) view.getLayoutParams();
            linearParams.height = STATE_BAR_HEIGHT;
            view.setLayoutParams(linearParams);

        }

        return tvTitleName;
    }
    public static TextView showTitleNameByRl2(Activity activity, String titleName) {
        TextView tvTitleName = (TextView) activity.findViewById(R.id.title_tv);
        tvTitleName.setText(titleName);
        tvTitleName.setVisibility(View.VISIBLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (STATE_BAR_HEIGHT <= 0) {
                STATE_BAR_HEIGHT = getStatusHeight(activity);
            }
            Window window = activity.getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            View view = activity.findViewById(R.id.state_bar);
            view.setVisibility(View.VISIBLE);
            LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) view.getLayoutParams();
            linearParams.height = STATE_BAR_HEIGHT;
            view.setLayoutParams(linearParams);

        }

        return tvTitleName;
    }

    public static void setStateHeight(Activity activity, View view, int titleNameId, View mainView) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (STATE_BAR_HEIGHT <= 0) {
                STATE_BAR_HEIGHT = getStatusHeight(activity);
            }
            Window window = activity.getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            view.setVisibility(View.VISIBLE);
            LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) view.getLayoutParams();
            // Log.e("titlebar", "STATE_BAR_HEIGHT：：：" + STATE_BAR_HEIGHT);
            linearParams.height = STATE_BAR_HEIGHT;
            view.setLayoutParams(linearParams);
        } else {
            // Log.e("titlebar", "版本过低：：：");
            view.setVisibility(View.GONE);
        }
        TextView tvTitleName = (TextView) mainView.findViewById(R.id.title_tv);
        tvTitleName.setText(titleNameId);
        tvTitleName.setVisibility(View.VISIBLE);
//        StatusBarUtil.StatusBarLightMode(activity);
    }

    public static void setStateHeight(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (STATE_BAR_HEIGHT <= 0) {
                STATE_BAR_HEIGHT = getStatusHeight(activity);
            }
            Window window = activity.getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        } else {
            // Log.e("titlebar", "版本过低：：：");
        }
        StatusBarUtil.StatusBarLightMode(activity);
    }

    /**
     * 显示标题
     *
     * @param activity    当前的activity
     * @param titleNameId 标题id
     */
    public static TextView showTitleName(Activity activity, int titleNameId) {
        TextView tvTitleName = (TextView) activity.findViewById(R.id.title_tv);
        tvTitleName.setText(titleNameId);
        tvTitleName.setVisibility(View.VISIBLE);
        setStateHight(activity);
        return tvTitleName;
    }

    /**
     * 显示标题
     *
     * @param activity    当前的activity
     * @param titleNameId 标题id
     */
    public static TextView showTitleName2(Activity activity, int titleNameId) {
        TextView tvTitleName = (TextView) activity.findViewById(R.id.title_tv2);
        tvTitleName.setText(titleNameId);
        tvTitleName.setVisibility(View.VISIBLE);
        setStateHight(activity);
        return tvTitleName;
    }

    /**
     * 显示标题
     *
     * @param activity    当前的activity
     * @param titleNameId 标题id
     */
    public static TextView showTitleNameStr(Activity activity, String titleNameId) {
        TextView tvTitleName = (TextView) activity.findViewById(R.id.title_tv2);
        tvTitleName.setText(titleNameId);
        tvTitleName.setVisibility(View.VISIBLE);
        setStateHight(activity);
        return tvTitleName;
    }

    /**
     * 显示标题
     *
     * @param activity    当前的activity
     * @param titleNameId 标题id
     */
    public static TextView showTitleName3(Activity activity, String titleNameId) {
        TextView tvTitleName = (TextView) activity.findViewById(R.id.title_tv2);
        tvTitleName.setText(titleNameId);
        tvTitleName.setVisibility(View.VISIBLE);
        setStateHight(activity);
        return tvTitleName;
    }

    /**
     * 显示标题
     *
     * @param activity    当前的activity
     * @param titleNameId 标题id
     */
    public static TextView showTitleName2(Activity activity, String titleNameId) {
        TextView tvTitleName = (TextView) activity.findViewById(R.id.title_tv);
        tvTitleName.setText(titleNameId);
        tvTitleName.setTextColor(R.color.textBlack);
        tvTitleName.setVisibility(View.VISIBLE);
        setStateHight(activity);
        return tvTitleName;
    }

    public static TextView showTitleName(Object obj, int titleNameId) {
        TextView tvTitleName = (TextView) ((View) obj).findViewById(R.id.title_tv);
        tvTitleName.setText(titleNameId);
        tvTitleName.setVisibility(View.VISIBLE);

        // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        // Window window = getWindow();
        // window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
        // WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        // }
        return tvTitleName;
    }

    // 左右按钮========================================================================

    /**
     * 显示左边Button按钮
     *
     * @param activity 当前的activity
     * @return
     */
    public static Button showBtnLeft(Activity activity, int stringId) {
        Button btnLeft = (Button) activity.findViewById(R.id.title_btn_left);
        btnLeft.setText(stringId);
        btnLeft.setVisibility(View.VISIBLE);
        return btnLeft;
    }

    /**
     * 显示左边Button按钮
     *
     * @param activity 当前的activity
     * @return
     */
    public static Button showBtnLeftTwo(Activity activity, int stringId) {
        Button btnLeft = (Button) activity.findViewById(R.id.title_btn_left);
        btnLeft.setText(stringId);
        btnLeft.setTextColor(activity.getResources().getColor(R.color.white));
        btnLeft.setVisibility(View.VISIBLE);
        return btnLeft;
    }

    /**
     * 显示右边的Button按钮
     *
     * @param activity 当前的activity
     * @return
     */
    public static Button showBtnRight(Activity activity, int stringId) {
        Button btnRight = (Button) activity.findViewById(R.id.title_btn_right);
        btnRight.setText(stringId);
        btnRight.setVisibility(View.VISIBLE);
        return btnRight;
    }

    /**
     * 显示右边的Button按钮
     *
     * @param activity 当前的activity
     * @return
     */
    public static Button showBtnRight2(Activity activity, int stringId) {
        Button btnRight = (Button) activity.findViewById(R.id.title_btn_right2);
        btnRight.setText(stringId);
        btnRight.setVisibility(View.VISIBLE);
        return btnRight;
    }

    /**
     * 显示左边的ImageButton按钮
     *
     * @param activity   当前的activity
     * @param drawableId iamgebutton的图标按钮
     * @return
     */
    public static ImageButton showIbLeft(Activity activity, int drawableId) {
        ImageButton btnLeft = (ImageButton) activity.findViewById(R.id.title_ib_left);
        btnLeft.setVisibility(View.VISIBLE);
        btnLeft.setImageResource(drawableId);
        return btnLeft;
    }

    public static ImageButton showIbLeft(Object obj, int drawableId) {
        ImageButton btnLeft = (ImageButton) ((View) obj).findViewById(R.id.title_ib_left);
        btnLeft.setVisibility(View.VISIBLE);
        btnLeft.setImageResource(drawableId);
        return btnLeft;
    }


    /**
     * 显示右边的ImageButton按钮，要传ID改变显示
     *
     * @param activity   当前的activity
     * @param drawableId imageButton的图标按钮
     * @return
     */
    public static ImageButton showIbRight(Activity activity, int drawableId) {
        ImageButton btnRight = (ImageButton) activity.findViewById(R.id.title_ib_right);
        btnRight.setVisibility(View.VISIBLE);
        btnRight.setImageResource(drawableId);
        return btnRight;
    }

    public static ImageButton showIbRight(Object obj, int drawableId) {
        ImageButton btnRight = (ImageButton) ((View) obj).findViewById(R.id.title_ib_right);
        btnRight.setVisibility(View.VISIBLE);
        btnRight.setImageResource(drawableId);
        return btnRight;
    }

    /**
     * 显示右边的ImageButton按钮，右边靠内的按钮
     *
     * @param activity   当前的activity
     * @param drawableId imageButton的图标按钮
     * @return
     */
    public static ImageButton showIbRight2(Activity activity, int drawableId) {
        ImageButton btnRight = (ImageButton) activity.findViewById(R.id.title_ib_right2);
        btnRight.setVisibility(View.VISIBLE);
        btnRight.setImageResource(drawableId);
        return btnRight;
    }

    public static ImageButton showIbRight3(Activity activity, int drawableId) {
        ImageButton btnRight = (ImageButton) activity.findViewById(R.id.title_ib_right3);
        btnRight.setVisibility(View.VISIBLE);
        btnRight.setImageResource(drawableId);
        return btnRight;
    }

    /**
     * 显示右边的ImageButton按钮，右边靠内的按钮
     *
     * @param activity   当前的activity
     * @param drawableId imageButton的图标按钮
     * @return
     */
    public static ImageButton showIbRight4(Activity activity, int drawableId) {
        ImageButton btnRight = (ImageButton) activity.findViewById(R.id.title_ib_right4);
        btnRight.setVisibility(View.VISIBLE);
        btnRight.setImageResource(drawableId);
        return btnRight;
    }

    /**
     * 使用资源文件的设置下一页返回按钮的文本
     *
     * @param intent
     * @param context
     * @param currentTitle
     */
    public static void setNext_pageBackBtn(Intent intent, Context context, int currentTitle) {
        intent.putExtra(MyTitlebarUtil.BACKTITLE, context.getString(currentTitle));
    }

    /**
     * 显示左边的ImageButton按钮
     *
     * @param activity   当前的activity
     * @param drawableId iamgebutton的图标按钮
     * @return
     */
    public static ImageButton showIbHome(Activity activity) {
        ImageButton title_home_right = (ImageButton) activity.findViewById(R.id.title_home_right);
        title_home_right.setVisibility(View.VISIBLE);
        return title_home_right;
    }

    /**
     * 获取手机状态栏的高度 返回大于0的数表示获取成功，小于0的表示获取失败。
     *
     * @param activity
     * @return
     */
    public static int getStatusHeight(Activity activity) {
        int statusHeight = 0;
        Rect localRect = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(localRect);
        statusHeight = localRect.top;
        if (0 == statusHeight) {
            Class<?> localClass;
            try {
                localClass = Class.forName("com.android.internal.R$dimen");
                Object localObject = localClass.newInstance();
                int i5 = Integer.parseInt(localClass.getField("status_bar_height").get(localObject)
                        .toString());
                statusHeight = activity.getResources().getDimensionPixelSize(i5);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (NumberFormatException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
        return statusHeight;
    }

}

